package top.malegod.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
import javax.servlet.http.HttpSession;
import top.malegod.model.User;

/**
 * Create by IntelliJ IDEA
 *
 * @author: AbelEthan
 * @date-Time: 2018/5/21 14:08
 * @description:  密码加密与生成uuid
 */
public class Utils {

  public static String md5(String plainText) {
    byte[] secretBytes = null;
    try {
      secretBytes = MessageDigest.getInstance("md5").digest(
          plainText.getBytes());
    } catch (NoSuchAlgorithmException e) {
      throw new RuntimeException("找不到md5算法");
    }
    String md5code = new BigInteger(1, secretBytes).toString(16);

    for (int i = 0; i < 32 - md5code.length(); i++) {
      md5code = "0" + md5code;
    }
    return md5code.toUpperCase();
  }

  public static String createUUID(){
    String uuid = UUID.randomUUID().toString();
    uuid = uuid.replace("-", "");
    return uuid;
  }

  public static ServerResponse login(HttpSession session){
    User user = (User) session.getAttribute("user");
    if (user == null){
      return ServerResponse.createError("用户未登录，请先登录");
    }
    return null;
  }

}
